<?php
// {{{ class PEAR2_LightDBException
class PEAR2_LightDBException extends Exception 
{
    const FAILED_TO_CONNECT = 'Could not connect to database';
    const QUERY_FAILED      = 'Query Failed';
}
// }}}
// {{{ class PEAR2_LightDBUtilities
class PEAR2_LightDBUtilities
{
    // {{{ public static function loadClass
    /**
     * Load class
     *
     * @param string $className  The class name to load.
     */
    public static function loadClass($className)
    {
        $class = 'PEAR2_LightDB_' . $className;
        
        if (!PEAR2_LightDBUtilities::classExists($className)) {
            $include =  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Driver' . 
                        DIRECTORY_SEPARATOR . $className . '.php';
                        
            if (file_exists($include)) {
                require $include;
                return new $class;
            }
            throw new PEAR2_LightDBException('RDBMS specified is not installed');
        }
    }
    // }}}
    // {{{ public static function classExists
    /**
     * Class exists verification.
     *
     * This method will check to see if the class exists, if it
     * does, then it'll return true, if not, then return false.
     *
     * @param string $className  The name of the class to load.
     */
    public static function classExists($className)
    {
        $class = 'PEAR2_LightDB_' . $className;
        if (class_exists($class)) {
            return true;
        }

        return false;
    }
    // }}}
}
// }}}
// {{{ class PEAR2_LightDBProperties
/**
 * PEAR2_LightDBProperties
 *
 * This class contains all the properties and global
 * informations we need accross the queries, etc.
 *
 * @version $Id$
 * @author  David Coallier <davidc@agoraproduction.com>
 */
class PEAR2_LightDBProperties
{
    /**
     * Default DSN information
     */
    public static $defaultDSN = array(
        'phptype'    =>   false,
        'dbsyntax'   =>   false,
        'username'   =>   false,
        'password'   =>   false,
        'protocol'   =>   false,
        'hostspec'   =>   false,
        'port'       =>   false,
        'socket'     =>   false,
        'database'   =>   false,
        'mode'       =>   false,
    );

    /**
     * Are we connected ? Hmm can't remember, this will help me do so.
     */
    public static $isConnected = false;

    /**
     * The fetchmodes
     */
    const FETCHMODE_ASSOC = 1;
    const FETCHMODE_ARRAY = 2;
    const FETCHMOD_OBJECT = 3;
    
}
// }}}
// {{{ class PEAR2_LightDB
/**
 * PEAR2_LightDB
 *
 * This class provides an easy and fast access to a small and simple database
 * wrapper with not many features to support cross features accross database
 * platforms. The main goal of this database access layer is to make easy to
 * create applications and switch database to execute queries.
 *
 * @version $Id$
 * @author David Coallier <davidc@agoraproduction.com>
 */
class PEAR2_LightDB
{
    // {{{ Properties
    /**
     * Data source
     *
     * @access protected
     */
    protected $_dsn;
    // }}}
    // {{{ Constructor ($dsn)
    /**
     * Constructor
     *
     * @param mixed $dsn  The datasource.
     */
    public function __construct($dsn)
    {
        $this->_dsn = self::parseDSN($dsn);
    }
    // }}}
    // {{{ public static function parseDSN($dsn)
    /**
     * Parse DSN
     *
     * Parse the DSN to connect to the user's datasource
     *
     * @param mixed $dsn   The datasource as an array or string.
     * @see LightDBProperties
     */
    public static function parseDSN($dsn)
    {
        $parsed = PEAR2_LightDBProperties::$defaultDSN;
        
        if (is_array($dsn)) {
            $dsn = array_merge($parsed, $dsn);
            if (!$dsn['dbsyntax']) {
                $dsn['dbsyntax'] = $dsn['phptype'];
            }
            return $dsn;
        }
    }
    // }}}
    // {{{ public static function factory($dsn, $options = false)
    /**
     * Factory method
     *
     * This method returns an object of the driver we want to use.
     */
    public static function factory($dsn, $options = false)
    {
        $dsninfo = PEAR2_LightDB::parseDSN($dsn);
        
        if (empty($dsninfo['phptype'])) {
            throw new PEAR2_LightDBException('No RDBMS Driver specified');
        }
        
        $db = PEAR2_LightDBUtilities::loadClass($dsninfo['phptype']);

        $db->setDSN($dsninfo);

        return $db;
    }
    // }}}
    // {{{ public static function connect
    /**
     * Connect
     *
     * @throws LightDBException
     */
    public static function connect($dsn, $options = false)
    {
        $db = self::factory($dsn, $options);
        
        if (!$db->connect()) {
            throw new PEAR2_LightDBException('Could not connect');
        }

        return $db;
    }
    // }}}
}
// }}}
